package com.lanou3g.code0515.study;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/15;
 * Otherwise, I don't know who created it either
 */

@WebServlet(name = "UploadServlet",urlPatterns = "/upload")
// 我们也可以通过加上MultipartConfig注解
// 来获取表单中的 其他input
// 建议在处理完上传之后,转发到一个单独的servlet去处理表单
// 中其他的input,因为 使用了 该注解 有可能导致
// FileUpLoad 不好用
@MultipartConfig
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 上传
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String code = req.getParameter("code");
        resp.getWriter().println(code);

        String uploadPath = getServletContext()
                .getRealPath("")
                + File.separator + "upload";
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()){
            // 创建文件夹
            uploadDir.mkdirs();
        }

        ServletFileUpload upload = new ServletFileUpload();

        FileItemFactory factory = new DiskFileItemFactory();
        upload.setFileItemFactory(factory);

        upload.setHeaderEncoding("utf-8");

        try {
            List<FileItem> fileItems = upload.parseRequest(req);
            if (fileItems == null){
                return;
            }
            for (FileItem fileItem : fileItems) {
                if (fileItem.isFormField()){
                    // 不是一个文件,是一个表单数据
                    // input 中的name属性
//                    String fieldName = fileItem.getFieldName();
//                    // name 是文件名,当不传文件时 为null
//                    String name = fileItem.getName();
//                    // string 是input的值
//                    // getString 参数传递 编码格式
//                    // 解决乱码
//                    String s = fileItem.getString("utf-8");
//
//                    System.out.println("fieldName:" + fieldName);
//                    System.out.println("name:" + name);
//                    System.out.println("s" + s);
//                    resp.getWriter().println(s);
                    continue;
                }
                // 源文件名
                String fileName = fileItem.getName();
                System.out.println(fileName);
                // 处理后的文件名
                String afterName = TestUUID.handleFileName(fileName);
                // 创建文件壳子
                File file = new File(uploadDir,afterName);
                fileItem.write(file);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
